package main

import (
	"database/sql"
	"fmt"
	"log"
	"strconv"
	"time"

	"github.com/cch123/elasticsql"
	_ "github.com/go-sql-driver/mysql"
	"golang.org/x/net/context"
)

func main() {
	i := f1()
	log.Println(i)
	i = f2()
	log.Println(i)
	i = f3()
	log.Println(i)

	i = f4()
	log.Println(i)
	i = f5()
	log.Println(i)

	i = c()
	log.Println(i)

	l1 := &ListNode{
		Val: 2,
	}

	return
	var str = `select * from info where name like '%rest%'`

	dsl, db, err := elasticsql.Convert(str)
	log.Println("dsl:", dsl, "db:", db, "err:", err)

	db2, err := sql.Open("mysql", "root:12345678@tcp(localhost:3306)/user?charset=utf8")

	defer db2.Close()
	db2.SetMaxIdleConns(1)
	db2.SetMaxOpenConns(100)
	//rs,err:=db2.Exec("")

	insertStr := "insert into user_info(name,privince) values('%s','%s')"
	for i := 0; i < 100000000; i++ {
		log.Println("i", i)

		go db2.Exec(fmt.Sprintf(insertStr, strconv.Itoa(i), "成都"))

	}
	log.Println("finish")
	select {}

}

func f1() int {
	i := 5
	defer func() {
		//log.Println("defer f1:",i)
		i++
	}()
	//log.Println("f1:",i)
	return i
}

func f2() int {
	i := 5
	defer func() {
		//log.Println("defer f2:",i)
		i++
	}()

	//log.Println("f2:",i)
	return i

}

func f3() (result int) {
	defer func() {
		result++
	}()
	return 0
}

func f4() (r int) {
	t := 5
	defer func() {
		t = t + 5
	}()
	return t
}

func f5() (r int) {
	defer func(r int) {
		r = r + 5
	}(r)
	return 1

}

func c() (i int) {
	defer func() { i++ }()
	return 1
}

func f6() {
	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)

	defer cancel()

	ctx.Done()
}

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	ret := &ListNode{0, nil}
	tmp := ret
	jw, sum := 0, 0
	for (l1 != nil && l2 != nil) || jw != 0 {
		val1, val2 := 0, 0
		if l1 != nil {
			val1 = l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			val2 = l2.Val
			l2 = l2.Next
		}

		sum = val1 + val2 + jw
		jw = sum / 10
		tmp.Next = &ListNode{sum % 10, nil}
		tmp = tmp.Next
	}
	if l1 != nil {
		tmp.Next = l1
	}
	if l2 != nil {
		tmp.Next = l2
	}
	return ret.Next
}
